package com.jxud.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jxud.aop.MyLOg;
import com.jxud.mapper.EmpMapper;
import com.jxud.pojo.Emp;
import com.jxud.pojo.PageBean;
import com.jxud.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;


//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//        //1.获取总记录数
//        long count = empMapper.count();
//
//        //2.获取分页查询的结果
//        Integer start = (page - 1) * pageSize;
//        List<Emp> empList = empMapper.page(start, pageSize);
//
//        //3.封装pageBean对象
//        PageBean pageBean = new PageBean(count,empList);
//
//        return pageBean;
//    }

    //分页查询员工
    @MyLOg
    @Override
    public PageBean page(Integer page,
                         Integer pageSize,
                         String name, Boolean gender, LocalDate begin, LocalDate end) {
        //1,分页参数
        PageHelper.startPage(page, pageSize);

        //2、执行查询
        List<Emp> empList = empMapper.list(name, gender, begin, end);
        Page<Emp> p = (Page<Emp>) empList;    //强转成PageHelp的page类型

        //3.封装pageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());

        return pageBean;
    }

    // 删除员工
    @MyLOg  //自定义注解 切入点表达式使用
    @Override
    public void delete(Integer id) {
        empMapper.delete(id);
    }

    //添加员工
    @Override
    public void add(Emp emp) {
        emp.setUpdatedAt(LocalDateTime.now());
        emp.setCreatedAt(LocalDateTime.now());
        empMapper.add(emp);
    }

    @Override
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdatedAt(LocalDateTime.now());
        empMapper.update(emp);
    }


    @Override
    public Emp login(Emp emp) {
        return empMapper.getByUsernameAndPassword(emp);
    }
}
